#
# This file is part of Contentbus

# Contentbus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Contentbus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Contentbus.  If not, see <http://www.gnu.org/licenses/>.

from setuptools import setup, find_packages, Command
from os import path, listdir
from glob import glob
import sys

try:
    from pyflakes.scripts.pyflakes import main as pyflakes_main
except ImportError:
    pyflakes_main = None

try:
    from pep8 import _main as pep8_main
except ImportError:
    pep8_main = None

from contentbus.version import __version__


def files_in_path(basepath, extensions):
    """
    Find files with specified extension starting from a base path.

    @param basepath: the base path for the search
    @param extensions: a list of file extensions
    @return: an iterator yielding results
    """
    paths = (path.join(basepath, _path)
             for _path in listdir(basepath))

    for _path in paths:
        if path.isdir(_path):
            for name in files_in_path(_path, extensions):
                yield name
        if path.isfile(_path):
            if path.splitext(_path)[-1].lower() in extensions:
                yield _path


class LintCommand(Command):
    """Lint source files"""

    description = "lint source files"

    user_options = [('path=', 'p', "base path for search")]

    def initialize_options(self):
        self.path = "contentbus"

    def finalize_options(self):
        pass

    def run(self):
        files_to_lint = list(files_in_path(self.path, (".py",)))

        if pep8_main is not None:
            sys.stderr.write("=== pep8 ===\n")
            sys.argv = [sys.argv[0]]
            sys.argv.extend(files_to_lint)
            pep8_main()

        if pyflakes_main is not None:
            sys.stderr.write("\n=== pyflakes ===\n")
            pyflakes_main(files_to_lint)


config = {
    "name": "contentbus",
    "version": __version__,
    "license": "GPL",
    "description": "Content-based bus",
    "long_description": open("README").read(),
    "author": "Alberto Donato",
    "author_email": "<alberto.donato@gmail.com>",
    "maintainer": "Alberto Donato",
    "maintainer_email": "<alberto.donato@gmail.com>",
    "url": "http://code.google.com/p/contentbus",
    "download_url": "http://code.google.com/p/contentbus/downloads/list",
    "install_requires": ["Twisted"],
    "packages": find_packages(exclude=["*.tests.*"]),
    "include_package_data": True,
    "scripts": glob(path.join("bin", "*")),
    "keywords": "content content-based bus publish subscribe",
    "classifiers": [
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: GNU General Public License (GPL)",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Application Frameworks",
        "Topic :: Software Development :: Libraries :: Python Modules"],
    "cmdclass": {"lint": LintCommand}}

setup(**config)
